<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<meta http-equiv="Pragma" content="no-cache" />
	<title>充值记录</title>
	<link rel="stylesheet" href="css/weui.min.css" />
	<link rel="stylesheet" href="fonts/iconfont.css" />
	<link rel="stylesheet" href="js/swiper/css/swiper.min.css" />
	<link rel="stylesheet" href="css/style.css" />
	<script src="js/jquery-2.2.3.min.js"></script>
	<!-- <script src="js/weui.min.js"></script> -->
	<!-- <script src="js/swiper/js/swiper.min.js"></script> -->

</head>

<body ontouchstart>
	<div class='weui-conetent'>
		<div class='weui-cells' id="showMessageBox"></div>
	</div>
</body>
<script src="./api.js"></script>
<script>


	window.onload = function () {

		let api = getApi()
		let userinfo = JSON.parse(localStorage.getItem('wxUserInfo')).data
		let customerID = userinfo.customer.customerID
		console.log(userinfo.customer.customerID)
		// 获取token
		let token = userinfo.token
		//请求消费明细参数
		let params = {
			currentPage: 1,
			pageSize: 8
		}

		let pages = ''

		//初始化请求消费明细 默认请求 currentPage: 1  pageSize: 8
		function initGetAccountAmount() {
			fetch(api + '/consumingRecords/' + customerID + '/3/' + params.currentPage + '/' + params.pageSize, {
				method: "get",
				headers: {
					'Content-type': 'application/json',
					"Authorization": token
				}
			})
				.then(res => res.json())
				.then((data) => {
					console.log(data.data.list)
					pages = data.data.pages
					var html = ''
					if (data.data.list.length > 0) {
						data.data.list.forEach((item, index) => {
							html += `<div class="weui-cell jl-item" style="padding:16px;">
									<div class="weui-cell__hd">
										<div class='number'>${item.price}</div>
										<div class="date">${item.operateTime}</div>
									</div>
									<div class="weui-cell__bd cont">
											${item.note}
									</div>
								</div>`
						})
						$('#showMessageBox').append(html)
					} else {
						$('#showMessageBox').append(`<div style=" width: 100%; text-align: center; line-height: 40px;">暂无明细</div>`)
					}
				}).catch((err) => {
					window.location.href = './index.html'
				})
		}
		//调用初始化请求明细
		initGetAccountAmount(params)

		// 获取消费明细 并将获取消费明细 插入页面
		// / consumingRecords / { customerID } / { consumingTypeID } / { currentPage } / { pageSize }

		// { consumingTypeID }: 消费1  退款是2  现金充值3 福利金发放4  福利金充值5
		function getAccountAmount(params) {
			fetch(api + '/consumingRecords/' + customerID + '/3/' + params.currentPage + '/' + params.pageSize, {
				method: "get",
				headers: {
					'Content-type': 'application/json',
					"Authorization": token
				}
			})
				.then(res => res.json())
				.then((data) => {
					console.log(data.data.list)
					var html = ''

					data.data.list.forEach((item, index) => {
						html += `<div class="weui-cell jl-item">
									<div class="weui-cell__hd">
										<div class='number'>${item.price}</div>
										<div class="date">${item.operateTime}</div>
									</div>
									<div class="weui-cell__bd cont">
											${item.note}
									</div>
								</div>`
					})
					$('#showMessageBox').append(html)
				})
		}

		//监听滚动事件
		$(window).scroll(function () {

			var scrollH = document.documentElement.scrollHeight

			var clientH = document.documentElement.clientHeight
			if (scrollH == (document.documentElement.scrollTop | document.body.scrollTop) + clientH + 100) {
				//加载新数据
				// alert("加载新数据")
				params.currentPage++
				if (pages > params.currentPage) {
					getAccountAmount(params)
				} else if (pages == params.currentPage) {
					getAccountAmount(params)
					console.log('0')
					// $('#showMessageBox').append(`<div style=" width: 100%; text-align: center; line-height: 40px;">没有了</div>`)
				} else {
					console.log('没有了')
				}
			}
		})
	}
</script>

</html>